package com.domino.server.persistence.playeraudit;

import javax.persistence.EntityManager;

import com.domino.server.cache.GameSessionData;
import com.domino.server.cache.State;
import com.domino.server.persistence.EMF;
import com.domino.shared.session.SessionId;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class PlayerDaoImpl implements PlayerDao {

	@Override
	public PlayerDO find(String id) {
		EntityManager em = null;
		try {
			em = EMF.create();
			Key key = KeyFactory.createKey(PlayerDO.class.getSimpleName(), Long.parseLong(id));
			PlayerDO pdo = em.find(PlayerDO.class, key);
			return pdo;
		} finally {
			if (em!=null) em.close();
		}
	}

	@Override
	public void create(PlayerDO pdo) {
		EntityManager em = null;
		try {
			em = EMF.create();
			em.getTransaction().begin();
			em.persist(pdo);
			em.getTransaction().commit();
		} finally {
			if (em!=null) em.close();
		}
	}

	@Override
	public void update(PlayerDO pdo) {
		EntityManager em = null;
		try {
			em = EMF.create();
			em.merge(pdo);
		} finally {
			if (em!=null) em.close();
		}
	}

}
